<?php
session_start();
include_once("../../untils/conn.php");
mysqli_query($con, "set names utf8");

// 获取当前正在登录的账号信息的代理
$proxy_acc = $_SESSION["account"];

// 查询代理的所有信息字段
$sql = "SELECT * FROM `proxy` WHERE `proxy_acc`='$proxy_acc'";
$result = mysqli_query($con, $sql);

// 取出 group_id 并存为变量
if ($result && $row = mysqli_fetch_assoc($result)) {
  $group_id = $row["group_id"];
  $puid = $row["proxy_id"];
   
}

// 查询代理所在的代理组信息
$sql = "SELECT * FROM `proxy_group` WHERE `group_id`=$group_id";
$result = mysqli_query($con, $sql);

// 取出 group_portion 并存为变量
if ($result && $row = mysqli_fetch_assoc($result)) {
  $group_portion = $row["group_portion"];
  $portion = $group_portion;
}

// 查询 goodsta 表，如果有 puid=$puid 的数据，则取出 spid 和 status 存为变量
$sql = "SELECT * FROM `goodsta` WHERE `puid`=$puid";
$result = mysqli_query($con, $sql);

// 定义一个空数组，用来保存 goodsta 表中的数据
$goodsta_data = array();

while ($result && $row = mysqli_fetch_assoc($result)) {
  $spid = $row["spid"];
  $status = $row["status"];

  // 将记录存入数组中
  $goodsta_data[] = array(
    'spid' => $spid,
    'status' => $status
  );
}

// 获取所有记录
$sql = "SELECT * FROM list WHERE state=0";
$result = mysqli_query($con, $sql);

// 定义一个空数组，用来保存查询结果
$data = array();

// 逐条取出记录，存为变量
while ($result && $row = mysqli_fetch_assoc($result)) {
  $id = $row['id'];
  $name = $row['name'];
  $intro = $row['jieshao'];
  $zhutu = $row['zhutu'];
  $yys  = $row['yys'];
  $gsd  = $row['gsd'];
  $proxy_price = $row['proxy_price'];
  $notes = $row['notes'];

  $state = 0;

  // 遍历 goodsta_data 数组，查找 spid 等于当前记录 id 的记录
  foreach ($goodsta_data as $goodsta) {
    if ($goodsta['spid'] == $id) {
      $state = $goodsta['status'] == 1 ? $goodsta['status'] : 0;
      break;
    }
  }

  $price = $proxy_price - $portion;

  // 将记录存入数组中
  $data[] = array(
    'id' => $id,
    'name' => $name,
    'intro' => $intro,
    'zhutu' => $zhutu,
    'yys' => $yys,
    'gsd' => $gsd,
    'price' => $price,
    'notes' => $notes,
    'state' => $state
  );
}

// 将数组转换为 JSON 格式并输出
echo json_encode($data);


?>
